<template>
    <section>
        <!--高级查询-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="employee">
                <el-form-item>
                    <el-input v-model="employee.username" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-select v-model="employee.state" placeholder="请选择">
                        <el-option label="全部" value=""></el-option>
                        <el-option label="在职" value=1></el-option>
                        <el-option label="离职" value=0></el-option>
                        <el-option label="待定" value=-1></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="searchDepartment">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="insertSubmit">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--员工列表加载-->
        <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <!--type="selection"选择多行数据-->
            <el-table-column type="selection">
            </el-table-column>
            <el-table-column type="index" align="center" label="序号" width="80">
            </el-table-column>
            <el-table-column prop="username" align="center" label="姓名">
            </el-table-column>
            <el-table-column prop="email" align="center" label="邮箱">
            </el-table-column>
            <el-table-column prop="phone" align="center" label="电话">
            </el-table-column>
            <el-table-column prop="age" align="center" label="年龄">
            </el-table-column>
            <el-table-column prop="state" align="center" label="在职状态" :formatter="formatState">
            </el-table-column>
            <el-table-column label="操作" width="150" >
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--分页和批量删除-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--增加和修改模态框-->
        <el-dialog title="修改" :visible.sync="dialogVisible"  width="30%">
            <!--修改弹窗-->
            <el-form :model="employeeForm" :rules="rules" ref="employeeForm" label-width="100px">
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="employeeForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="employeeForm.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="employeeForm.email"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="employeeForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model.number="employeeForm.age"></el-input>
                </el-form-item>
                <!--启用状态下拉框-->
                <el-form-item label="启用状态" prop="state">
                    <el-radio-group v-model="employeeForm.state">
                        <el-radio class="radio" :label=1>在职</el-radio>
                        <el-radio class="radio" :label=0>离职</el-radio>
                        <el-radio class="radio" :label=-1>待定</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click.native="addSubmit" >确 定</el-button>
          </span>
        </el-dialog>

    </section>
</template>
<script>
    export default {
        data() {
            return {
                //员工列表
                employees:[],
                //加载等待
                listLoading:false,
                pageSize:10,
                total:0,
                currentPage:1,
                sels:[],
                ids:[],
                employee:{
                    username:"",
                    state:""
                },
                dialogVisible:false,
                employeeForm:{
                    username:"",
                    password:"",
                    email:"",
                    phone:"",
                    age:"",
                    state:""
                },
                rules: {
                    username: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, max: 200, message: '长度在 6 到 200 个字符', trigger: 'blur'}
                    ],
                    email:[
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']
                        },{
                        pattern:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
                        }
                    ],
                    age: [
                        { required: true, message: '年龄不能为空'},
                        { type: 'number', message: '年龄必须为数字值'}
                    ],
                    phone: [
                        {
                            required: true,
                            message: "请输入手机号码",
                            trigger: "blur"
                        },{
                            pattern:/^1[34578]\d{9}$/
                        }
                    ],
                }
            }
        },
        methods:{
            //多行改变事件
            selsChange(sels){
                console.debug(sels)
                this.sels=sels;
            },
            formatState(row,column){
              return  row.state==1?"在职":(row.state==0?"离职":"待定");
            },
            //查询
            searchDepartment(){
                this.loadEmployeesByQuery();
            },
            //增加
            insertSubmit(){
                //this.$refs[employeeForm].resetFields();
                //弹出模态框
                this.dialogVisible=true;
                for (var k in this.employeeForm){
                    this.employeeForm[k] = "";
                }
                this.employeeForm.state = 1;
            },
            //编辑事件
            handleEdit(index,row){
                this.dialogVisible=true;
                for(var k in row){
                    console.debug("=======================",row[k])
                    this.employeeForm[k] = row[k];
                }
            },
            //弹出框确定按钮
            addSubmit(){
                this.$refs["employeeForm"].validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            let param = this.employeeForm;
                            if(param.id){
                                this.$http.post("http://localhost/employee",param).then((result=>{
                                    if(result.data.success){
                                        this.$message({
                                            type: 'success',
                                            message: '修改成功!'
                                        });
                                        this.loadEmployeesByQuery();
                                    }else {
                                        this.$message.error(result.data.msg);
                                    }
                                    this.dialogVisible=false;
                                }))
                            }else{
                                this.$http.put("http://localhost/employee",param).then((result=>{
                                    console.debug("=============",result)
                                    if(result.data.success){
                                        this.$message({
                                            type: 'success',
                                            message: '增加成功!'
                                        });
                                        this.loadEmployeesByQuery();
                                    }else {
                                        this.$message.error(result.data.msg);
                                    }
                                    this.dialogVisible=false;
                                }))
                            }
                        })
                    }
                })

            },
            //删除事件
            handleDel(index,row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送请求，传入id参数
                    this.$http.delete("http://localhost/employee/"+row.id).then((result=>{
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.loadEmployeesByQuery();
                        }else{
                            this.$message.error(result.data.msg);
                        }
                    }))
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //批量删除
            batchRemove(){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送请求，传入id参数
                    for (var s in this.sels){
                         this.ids[s] = this.sels[s].id;
                    }
                    this.$http.patch("http://localhost/employee/batchRemove",this.ids).then((result=>{
                        console.debug(result)
                        if(result.data.success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.loadEmployeesByQuery();
                        }else{
                            this.$message.error(result.data.msg);
                        }
                    }))
                })
            },
            //改变当前页事件
            handleCurrentChange(val){
                this.currentPage=val;
                this.loadEmployeesByQuery();
            },
            //加载列表数据
            loadEmployeesByQuery(){
                //分页参数
                let param = {"currentPage":this.currentPage,"pageSize":this.pageSize,"username":this.employee.username,"state":this.employee.state}
                this.$http.patch("http://localhost/employee",param).then((result)=>{
                        console.debug(result)
                        //后台返回员工数据列表
                        this.employees=result.data.data;
                        //后台返回员工数据条数
                        this.total = result.data.total;
                })
            }
        },
        mounted(){
            this.loadEmployeesByQuery();
        }
    }
</script>